#Creation of two datasets for confirmed cases for both time periods : T1, T2

province_level_df_half1 <- province_level_df[1:18,]
province_level_df_half2 <- province_level_df[19:36,]

############

#Calcul of Rproxy values for the 1st time period with d=5
R0_5_provinces_half1 <- calcul_R0(df = province_level_df_half1,time_window = 5)$R0_fin
R0_5_provinces_half1_full <- calcul_R0(df = province_level_df_half1,time_window = 5)[[2]]
R0_5_provinces_half1 <- R0_5_provinces_half1[-1,]

#Calcul of Rproxy values for the 2nd time period with d=5
R0_5_provinces_half2 <- calcul_R0(df = province_level_df_half2,time_window = 5)$R0_fin
R0_5_provinces_half2_full <- calcul_R0(df = province_level_df_half2,time_window = 5)[[2]]
R0_5_provinces_half2 <- R0_5_provinces_half2[-1,]

plot(R0_5_provinces_half1$R0,pch="+",col="blue",ylab="R0",xlab="Provinces",main="R0 per province (time window = 5 days)",ylim=c(0,max(R0_5_provinces_half1$R0,na.rm=T)))
points(R0_5_provinces_half2$R0,col="red")
legend("topright",col=c("blue","red"),c("First part of the dataset","Second part of the dataset"),lty=1)

#########

#Calcul of Rproxy values for the 1st time period with d=6
R0_6_provinces_half1 <- calcul_R0(df = province_level_df_half1,time_window = 6)$R0_fin
R0_6_provinces_half1_full <- calcul_R0(df = province_level_df_half1,time_window = 6)[[2]]
R0_6_provinces_half1 <- R0_6_provinces_half1[-1,]

#Calcul of Rproxy values for the 2nd time period with d=6
R0_6_provinces_half2 <- calcul_R0(df = province_level_df_half2,time_window = 6)$R0_fin
R0_6_provinces_half2_full <- calcul_R0(df = province_level_df_half2,time_window = 6)[[2]]
R0_6_provinces_half2 <- R0_6_provinces_half2[-1,]

plot(R0_6_provinces_half1$R0,pch="+",col="blue",ylab="R0",xlab="Provinces",main="R0 per province (time window = 6 days)",ylim=c(0,max(R0_5_provinces_half1$R0,na.rm=T)))
points(R0_6_provinces_half2$R0,col="red")
legend("topright",col=c("blue","red"),c("First part of the dataset","Second part of the dataset"),lty=1)

##########

#Calcul of Rproxy values for the 1st time period with d=7
R0_7_provinces_half1 <- calcul_R0(df = province_level_df_half1,time_window = 7)$R0_fin
R0_7_provinces_half1_full <- calcul_R0(df = province_level_df_half1,time_window = 7)[[2]]
R0_7_provinces_half1 <- R0_7_provinces_half1[-1,]

#Calcul of Rproxy values for the 2nd time period with d=7
R0_7_provinces_half2 <- calcul_R0(df = province_level_df_half2,time_window = 7)$R0_fin
R0_7_provinces_half2_full <- calcul_R0(df = province_level_df_half2,time_window = 7)[[2]]
R0_7_provinces_half2 <- R0_7_provinces_half2[-1,]

plot(R0_7_provinces_half1$R0,pch="+",col="blue",ylab="R0",xlab="Provinces",main="R0 per province (time window = 7 days)",ylim=c(0,max(R0_5_provinces_half1$R0,na.rm=T)))
points(R0_7_provinces_half2$R0,col="red")
legend("topright",col=c("blue","red"),c("First part of the dataset","Second part of the dataset"),lty=1)

save(R0_5_provinces_half1_full, file=paste("R0_5_provinces_half1_full", '.Rdata', sep=''))
save(R0_6_provinces_half1_full, file=paste("R0_6_provinces_half1_full", '.Rdata', sep=''))
save(R0_7_provinces_half1_full, file=paste("R0_7_provinces_half1_full", '.Rdata', sep=''))

save(R0_5_provinces_half2_full, file=paste("R0_5_provinces_half2_full", '.Rdata', sep=''))
save(R0_6_provinces_half2_full, file=paste("R0_6_provinces_half2_full", '.Rdata', sep=''))
save(R0_7_provinces_half2_full, file=paste("R0_7_provinces_half2_full", '.Rdata', sep=''))

############################################
# Calcul of the mean and median values to obtain our Rproxy for each province (1st time period)

colnames(R0_5_provinces_half1) <- c("Area","R0_5_half1")
colnames(R0_6_provinces_half1) <- c("Area","R0_6_half1")
colnames(R0_7_provinces_half1) <- c("Area","R0_7_half1")

R0_provinces_half1 <- merge(R0_5_provinces_half1,R0_6_provinces_half1)
R0_provinces_half1 <- merge(R0_provinces_half1,R0_7_provinces_half1)

R0_provinces_half1$mean <- apply(as.matrix(R0_provinces_half1[,2:4]),
                                     MARGIN=1,FUN=mean)

R0_provinces_half1$median <- apply(as.matrix(R0_provinces_half1[,2:4]),
                                       MARGIN=1,FUN=median)

############################################
# Calcul of the mean and median values to obtain our Rproxy for each province (2nd time period)

colnames(R0_5_provinces_half2) <- c("Area","R0_5_half2")
colnames(R0_6_provinces_half2) <- c("Area","R0_6_half2")
colnames(R0_7_provinces_half2) <- c("Area","R0_7_half2")

R0_provinces_half2 <- merge(R0_5_provinces_half2,R0_6_provinces_half2)
R0_provinces_half2 <- merge(R0_provinces_half2,R0_7_provinces_half2)

R0_provinces_half2$mean <- apply(as.matrix(R0_provinces_half2[,2:4]),
                                     MARGIN=1,FUN=mean)

R0_provinces_half2$median <- apply(as.matrix(R0_provinces_half2[,2:4]),
                                       MARGIN=1,FUN=median)

